//
//  MessageViewController.m
//  LubanWork
//
//  Created by xiaohao on 2019/12/31.
//  Copyright © 2019 xiaohao. All rights reserved.
//

#import "MessageViewController.h"
#import "messageTableViewCell.h"
#import "CustomerServiceViewController.h"
#import "MessageDetailViewController.h"
#import "systemViewController.h"
#import "RegisteredModel.h"
#import "GotoLoginView.h"
#import "systemViewController.h"
@interface MessageViewController ()<UITableViewDelegate,UITableViewDataSource>{
    MJRefreshAutoNormalFooter *footer;
}

@property(nonatomic,strong) UITableView * table;

@property(nonatomic,strong) NSMutableArray * titArray;

@property(nonatomic,assign) NSInteger page;

@property(nonatomic,strong) RegisteredModel * regModel;
@end

@implementation MessageViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.titArray = [[NSMutableArray alloc] init];
    self.page = 1;
    [self loadData];
    [self setTable];
}

-( void)setTable{
    [self setExtraCellLineHidden:self.table];
    [self.table setSeparatorInset:UIEdgeInsetsZero];
    [self.view addSubview:self.table];
    
}
- (void)setExtraCellLineHidden:(UITableView *)tab{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tab setTableFooterView:view];
}
-(UITableView *)table{
    if (!_table) {
        _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-kTabHeight) style:UITableViewStyleGrouped];
        _table.delegate = self;
        _table.dataSource = self;
        _table.backgroundColor = kColorFromRGB(247, 248, 255);
        _table.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.page = 1;
            [self loadData];
        }];
//        _table.mj_footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
//            self.page++;
//            [self loadData];
//        }];
        footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreVideo)];
        [footer setTitle:@"暂无更多数据" forState:MJRefreshStateRefreshing];
        [footer setTitle:@"暂无更多数据" forState:MJRefreshStateNoMoreData];
        [self->footer setTitle:@"" forState:MJRefreshStateIdle];
        footer.stateLabel.font = [UIFont systemFontOfSize:15.0f];
        footer.automaticallyHidden = YES;
        _table.mj_footer = footer;
    }
    return _table;
}
-(void)getMoreVideo{
    self.page ++;
    [self loadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else
    {
        return self.titArray.count;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    
    view.backgroundColor = kColorFromRGB(249, 247, 255);
    return view;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = [ NSString stringWithFormat:@"%ld%ld",(long)[indexPath section],(long)[indexPath row]];
    messageTableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[messageTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            [cell removeAllSubviews];
            UIImageView *leftImage = [[UIImageView alloc] init];
            leftImage.userInteractionEnabled = YES;
            leftImage.image = [UIImage imageNamed:@"message_leftImg"];
            [leftImage addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(leftImgClick)]];
            
            [cell addSubview:leftImage];
            [leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(kSCRATIO(25));
                make.top.mas_offset(KSCHEIGHT(14));
                make.width.mas_offset(kSCRATIO(150));
                make.height.mas_offset(kSCRATIO(50));
            }];
            UIImageView *rightImage = [[UIImageView alloc] init];
            rightImage.image = [UIImage imageNamed:@"callKeFu"];
            rightImage.userInteractionEnabled = YES;
            [rightImage addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rigImgClick)]];
            [cell addSubview:rightImage];
            [rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_offset(kSCRATIO(-25));
                make.top.mas_offset(KSCHEIGHT(14));
                make.width.mas_offset(kSCRATIO(150));
                make.height.mas_offset(kSCRATIO(50));
            }];
            UILabel *newLab = [UILabel CreatLabeltext:@"最新消息" Font:MarkFont(15) Textcolor:kColorFromRGBHex(0x333333) textAlignment:NSTextAlignmentLeft];
            [cell addSubview:newLab];
            [newLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(rightImage.mas_bottom).mas_offset(KSCHEIGHT(20));
                make.left.mas_offset(kSCRATIO(25));
            }];
            
        }
    }else{
        RegisteredModelResult *list = self.titArray[indexPath.row];
        cell.titLab.text = list.name;
        cell.timeLab.text = list.createTime;
        [cell.headerImg sd_setImageWithURL:[NSURL URLWithString:list.companyImage] placeholderImage:[UIImage imageNamed:@"userHeaderImg"]];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        RegisteredModelResult *list = self.titArray[indexPath.row];
        MessageDetailViewController *messageDetailVC = [[MessageDetailViewController alloc] init];
        messageDetailVC.NextModel = list;
        [self.navigationController pushViewController:messageDetailVC animated:YES];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return KSCHEIGHT(119);
    }else{
        return KSCHEIGHT(75);
    }
}
-(void)loadData{
    NSDictionary *dic = @{
        @"admissionStatus":@"1",
        @"pageNo":[NSString stringWithFormat:@"%ld",(long)self.page],
        @"pageSize":@"10",
    };
    __weak typeof(self) weakSelf = self;
    [[NetWorkManager sharedManager] getData:@"api/jianzhi/getSignupByUser" parameters:dic success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([responseObject[@"code"] isEqualToNumber:@200]) {
            NSArray *arr = responseObject[@"result"];
            if (weakSelf.page == 1) {
                [weakSelf.titArray removeAllObjects];
            }
            weakSelf.regModel = [RegisteredModel yy_modelWithJSON:responseObject];
            if (arr.count <= 0) {
                //                [weakSelf.table.mj_footer endRefreshingWithNoMoreData];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self->footer setTitle:@"暂无更多数据" forState:MJRefreshStateNoMoreData];
                    //                    if ([self.table.mj_footer isRefreshing]) {
                    //                        self.table.mj_footer.hidden = YES;
                    //                    }
                });
            }else{
                [weakSelf.titArray addObjectsFromArray:weakSelf.regModel.result];
            }
            //
            if (self.regModel.result.count > 0) {
                [weakSelf.table.mj_footer endRefreshingWithNoMoreData];
            }else{
                
            }
            if (arr == 0) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self->footer setTitle:@"暂无更多" forState:MJRefreshStateIdle];
                });
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.table reloadData];
                [weakSelf.table.mj_footer endRefreshing];
                [weakSelf.table.mj_header endRefreshing];
            });
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [HWProgressHUD showMessage:@"请联系管理员"];
            [weakSelf.table.mj_header endRefreshing];
            [weakSelf.table.mj_footer endRefreshing];
        });
    }];
}
-(void)rigImgClick{
    [self.navigationController pushViewController:[CustomerServiceViewController new] animated:YES];
}
-(void)leftImgClick{
    systemViewController *sVC = [[systemViewController alloc] init];
    [self.navigationController pushViewController:sVC animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat sectionHeaderHeight = 44;//这里的高度是设置的sectionView的高度
    if(scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        
    } else if (scrollView.contentOffset.y >= sectionHeaderHeight)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}
@end
